let util = require('../../utils/util');

let requestDomain = util.config('requestDomain');

let that;

Page({
    data: {
        profileId: '',
        profileName: '',
        oldProfileVal: '',
        profileVal: '',
        profileType: '',
        profileMaxLength: 18,
		submitBtnDisabled: false
    },
	onLoad: function(params) {
        that = this;
        let {profileId, profileName, profileVal, profileType, profileMaxLength} = params;
        wx.setNavigationBarTitle({
            title: '修改' + profileName
        });
        that.setData({
            profileId: profileId,
            profileName: profileName,
            oldProfileVal: (profileVal == '无' ? '' : profileVal),
            profileVal: (profileVal == '无' ? '' : profileVal),
            profileType: profileType,
            profileMaxLength: profileMaxLength
        });
    },
	profileValInput: function(e) {
        if (e.detail.value == '') {
            that.setData({ submitBtnDisabled: true });
        }
        else {
            that.setData({ submitBtnDisabled: false });
        }
		that.setData({profileVal: e.detail.value});
	},
	submitUpdate: function() {
        let {profileId, profileName, profileVal} = that.data;
		wx.showModal({
			title: '确认修改？',
			content: '您将修改' + profileName + '，是否继续？',
			success: function(modal) {
				if(modal.confirm) {
					wx.showNavigationBarLoading();
					that.setData({submitBtnDisabled: true});
					wx.request({
                        url: requestDomain + 'user/updateProfile',
						method: 'POST',
						data: {
							storeId: util.globalData('storeId'),
							_3rdSession: util.globalData('_3rdSession'),
							profileId: profileId,
							profileVal: profileVal
						},
						success: function (result) {
							wx.hideNavigationBarLoading();
							if(util.checkSuccess(result) && result.data && result.data.data) {
                                util.globalData('lastEditProfile', {
                                    profileId: profileId,
                                    profileVal: profileVal
                                });
                                wx.showToast({
                                    title: '修改成功',
                                    icon: 'success'
                                });
                                setTimeout(function() {
                                    wx.navigateBack();
                                }, 600);
                            }
                            else {
                                that.setData({ submitBtnDisabled: false });
                                util.showError('修改失败', '系统在处理你的修改时出现问题' + result.data.errMsg, result.data.errCode);
                            }
						},
						fail: function () {
							wx.hideNavigationBarLoading();
							that.setData({ submitBtnDisabled: false });
						}
					});
				}
			}
		});			
	}
});